package org.zhaya;

import cn.hutool.json.JSONUtil;
import io.logz.sawmill.*;
import io.logz.sawmill.processors.AddFieldProcessor;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.zhaya.sdk.PipelineServiceFactory;
import org.zhaya.sdk.builder.ConditionsBuilder;
import org.zhaya.sdk.builder.PipelineModelBuilder;
import org.zhaya.sdk.constant.LogicEnum;
import org.zhaya.sdk.model.PipelineDedugResult;
import org.zhaya.sdk.model.PipelineExecuteResult;
import org.zhaya.sdk.model.ServiceRequestParam;

import java.util.List;

@Slf4j
public class Demo1 {

    @Test
    public void executeTest1() {
        String modelJson = PipelineServiceFactory.pipelineModelBuilder().next(ProcessorBuildExample.addFieldProcessor).buildModelJson();

        List<PipelineDedugResult> debugResultList = PipelineServiceFactory.pipelineModelDebug(
                ServiceRequestParam.builder().modelJson(modelJson).debugNode(ProcessorBuildExample.addFieldProcessor).rawData("{\"a\": 2}").build());
        System.out.println(JSONUtil.toJsonStr(debugResultList));
    }

    @Test
    public void executeTest2() {

        /**
         * 数据
         * {
         * 	"a": 4,
         * 	"b": 4,
         * 	"c": 4,
         * 	"d": 4,
         * 	"fieldName": "a"
         * }
         */
        //拆分汇聚流程
        System.out.println("拆分汇聚流程config：");
        //条件 a>=3 & b>5
        ConditionsBuilder conditions1 = PipelineServiceFactory.conditionsBuilder()
                .logic(LogicEnum.AND)
                .ge("a", 3)
                .gt("b", 5);
        //条件 c>=7 & d>9
        ConditionsBuilder conditions2 = PipelineServiceFactory.conditionsBuilder()
                .logic(LogicEnum.AND)
                .ge("c", 7)//c>=7
                .gt("d", 9);//d>9
        PipelineModelBuilder ifBuilder = PipelineServiceFactory.pipelineModelBuilder()
                .when(conditions1).next(ProcessorBuildExample.splitProcessor).next(ProcessorBuildExample.dateProcessor)
                .when(conditions2).next(ProcessorBuildExample.addTagProcessor)
                .otherwise(ProcessorBuildExample.addFieldProcessor);
        String pipelineModel = PipelineServiceFactory.pipelineModelBuilder()
                .next(ProcessorBuildExample.upperCaseProcessor)
                .next(ifBuilder)
                .choose(ProcessorBuildExample.dateProcessor).next(ProcessorBuildExample.numberExchangeProcessor)
                .next(ProcessorBuildExample.convertProcessor)
                .union(ProcessorBuildExample.addTagProcessor, ProcessorBuildExample.convertProcessor)
                .next(ProcessorBuildExample.urldecodeProcessor)
                .buildModelJson();
        System.out.println(pipelineModel);
        PipelineExecuteResult executeResult = PipelineServiceFactory.pipelineModelExecute(
                ServiceRequestParam.builder().modelJson(pipelineModel).rawData("{\"a\":4,\"b\":4,\"c\":4,\"d\":4,\"fieldName\":\"a\"}").build());
        System.out.println(JSONUtil.toJsonStr(executeResult));
    }


}
